﻿/*************************************************************************
 *
 * File: Settings.js
 *
 * Description: 
 *
 ************************************************************************/

var rdp_settings = {'servers' : []}
var editMode = "add";  //can be "add" or "edit"
var editIndex = 0
 
// Delegate for the settings closing event. 
System.Gadget.onSettingsClosing = SettingsClosing;

var builder = new GadgetBuilder();
builder.Initialize();
var filesystem = builder.LoadType(System.Gadget.path + "\\bin\\RDPUtils.dll", "Utils.FileSystem");

// --------------------------------------------------------------------
// Initialize the settings.
// --------------------------------------------------------------------
function LoadSettings()
{
	try
	{	
		rdp_settings_json = filesystem.ReadFile(System.Gadget.path + "\\settings.txt");					
		System.Debug.outputString("Settings.LoadSettings: rdp_settings_json = " + rdp_settings_json);
	   
		if (rdp_settings_json)
		{			
			rdp_settings = JSON.parse(rdp_settings_json);
			PopulateServerTable();			
		}		
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.LoadSettings: " + e.description);
    }
}

function PopulateServerTable()
{
	try
	{
		System.Debug.outputString("Settings.PopulateServerTable: rdp_settings = " + rdp_settings);
		
		if (tableServers.rows)
		{
			//Delete rows from existing table
			while(tableServers.rows.length)
			{
				tableServers.deleteRow(0);
			}
			
			//Add first title row
			var row = tableServers.insertRow(0);
			var cell = row.insertCell(0);
			cell.innerHTML = "<input type='checkbox' onclick='btnSelectDeselectAll_Click(this.checked)'/>"
			cell.align = "center";
			cell.width = "10%";
			
			cell = row.insertCell(1);
			cell.innerHTML = "<i><b>Category</b></i>";
			cell.width = "25%";
			
			cell = row.insertCell(2);
			cell.innerHTML = "<i><b>Display Name</b></i>";		
		}
		
		//If we have servers then render them in the table
		if (rdp_settings.servers && rdp_settings.servers.length > 0)
		{
			rdp_settings.servers = rdp_settings.servers.sort(function(a,b) {
				var x = (a.category + a.displayName).toLowerCase();
				var y = (b.category + b.displayName).toLowerCase();
				return ((x < y) ? -1 : ((x > y) ? 1 : 0));
			});	
			
			for (var i=0; i<rdp_settings.servers.length; i++)
			{
				row = tableServers.insertRow(i+1);
				
				cell = row.insertCell(0);
				cell.align = "center";
				cell.innerHTML = "<input type='checkbox' class='checkboxes' id='checkbox" + (i+1) + "'/>";
				cell.name = "cell" + i;
				
				cell = row.insertCell(1);
				cell.innerHTML = rdp_settings.servers[i].category;			
				
				cell = row.insertCell(2);
				cell.innerHTML = "<a href='#' onclick='return btnEditServer_Click(" + i + ")'>" + rdp_settings.servers[i].displayName + "</a>";
			}
		}			
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.PopulateServerTable: " + e.description);
    }
}

// --------------------------------------------------------------------
// Handle the Settings dialog closing event.
// Parameters:
// event - event arguments.
// --------------------------------------------------------------------
function SettingsClosing(event)
{
	try
	{
		// Save the settings if the user clicked OK.
		if (event.closeAction == event.Action.commit)
		{
			var rdp_settings_json = JSON.stringify(rdp_settings,null,"\t");					
			filesystem.SaveToFile(rdp_settings_json, System.Gadget.path + "\\settings.txt");			
			System.Gadget.Settings.writeString("rdp_settings_json", rdp_settings_json);
			event.cancel = false;			
		}
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.SettingsClosing: " + e.description);
    }
}

//Event Functions 
function btnAddServer_Click()
{
	try
	{
		editMode = "add"
		errorMessage.innerText = "";
		serverListDiv.style.display = 'none';
		addEditServerDiv.style.display = 'block';
		serverName.focus();
		displayName.value = "";
		serverName.value = "";
		category.value = "";
		return false;
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.btnAddServer_Click: " + e.description);
    }
}

function btnEditServer_Click(index)
{
	try
	{
		editMode = "edit";
		editIndex = index;
		errorMessage.innerText = "";
		serverListDiv.style.display = 'none';
		addEditServerDiv.style.display = 'block';
		serverName.focus();
		displayName.value = rdp_settings.servers[index].displayName;
		serverName.value = rdp_settings.servers[index].serverName;
		category.value = rdp_settings.servers[index].category;
		return false;
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.btnEditServer_Click: " + e.description);
    }
}

function btnApply_Click()
{
	try
	{		
		if (serverName.value == "") 
		{
			errorMessage.innerText = "Server Name is required.";
			serverName.focus();
			return false;
		}
		
		if (displayName.value == "") 
		{
			errorMessage.innerText = "Display Name is required.";
			displayName.focus();
			return false;
		}
		
		if (editMode == "add")
		{
			rdp_settings.servers.push({"displayName":displayName.value, "serverName":serverName.value, "category":category.value});	
		}
		else
		{
			rdp_settings.servers[editIndex].displayName = displayName.value;
			rdp_settings.servers[editIndex].serverName = serverName.value;
			rdp_settings.servers[editIndex].category = category.value;
		}
	
		PopulateServerTable();
		serverListDiv.style.display = 'block';
		addEditServerDiv.style.display = 'none';
		System.Debug.outputString("Settings.btnApply_Click() called");
		return false;
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.btnApply_Click: " + e.description);
    }
}

function btnCancel_Click()
{
	try
	{
		serverListDiv.style.display='block';
		addEditServerDiv.style.display='none';	
		return false;		
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.btnCancel_Click: " + e.description);
    }
}

function btnDeleteServer_Click()
{
	try
	{
		System.Debug.outputString("Settings.btnDeleteServer_Click()");
		System.Debug.outputString("Settings.btnDeleteServer_Click: rdp_settings.servers.length = " + rdp_settings.servers.length);
		var should_populate_table = false;
		for (var index = rdp_settings.servers.length; index > 0 ; index--)
		{
			var checked = eval("checkbox" + index + ".checked");
			System.Debug.outputString("Settings.btnDeleteServer_Click: index, checked = " + index + "," + checked);
			if (checked)
			{
				should_populate_table = true;
				rdp_settings.servers.splice(index-1, 1);
			}
		}
		
		if (should_populate_table) PopulateServerTable();
		return false;
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.btnDeleteServer_Click: " + e.description);
    }
}

function btnSelectDeselectAll_Click(toggleState)
{
	try
	{			
		System.Debug.outputString("Settings.btnSelectDeselectAll_Click(): toggleState = " + toggleState);		
		System.Debug.outputString("Settings.btnSelectDeselectAll_Click(): tableServers.rows.length = " + tableServers.rows.length);
		
		if (tableServers.rows.length > 1)
		{					
			$(".checkboxes").each(function() {
				this.checked = toggleState;
			})
		}		
		
		return false;
	}
    catch(e)
    {
        System.Debug.outputString("ERROR:Settings.btnSelectDeselectAll_Click: " + e.description);
    }
}